<template>
  <div class="app-container">
    <!-- 头部header区域 -->
    <mt-header fixed title="我的Vue项目">
      <span to="/" slot="left">
        <mt-button icon="back" @click="back" v-show="flag">返回</mt-button>
      </span>
    </mt-header>

    <!-- 路由组件容器 -->
    <!-- 实现动画效果 -->
    <transition>
      <router-view></router-view>
    </transition>

    <!-- 底部导航 -->
    <footer class="mui-bar mui-bar-tab">
      <router-link class="my-tab-item " to="/home">
        <span class="mui-icon mui-icon-home"></span>
        <span class="mui-tab-label">首页</span>
      </router-link>
      <router-link class="my-tab-item" to="/member">
        <span class="mui-icon mui-icon-contact"></span>
        <span class="mui-tab-label">会员</span>
      </router-link>
      <router-link class="my-tab-item" to="/cart">
        <span class="mui-icon mui-icon-extra mui-icon-extra-cart">
          <span class="mui-badge">{{ totalCount }}</span>
        </span>
        <span class="mui-tab-label">购物车</span>
      </router-link>
      <router-link class="my-tab-item" to="/search">
        <span class="mui-icon mui-icon-search"></span>
        <span class="mui-tab-label">搜索</span>
      </router-link>
    </footer>
  </div>
</template>

<script>
import { mapGetters } from "vuex"
export default {
  methods: {
    // 一、实现点击返回按钮返回上一页的功能
    back() {
      // console.log(this)
      // 通过原型查找到的方法
      this.$router.go(-1)
    }
  },
  data() {
    return {
      // 控制返回按钮显示隐藏的标识符
      flag: true
    }
  },
  watch: {
    // 二、通过监听属性,监听地址栏($route.path)的变化,当跳转到 home 页面时就把标识符 flag 变成false
    "$route.path": {
      immediate: true,
      handler(newVal) {
        // console.log(newVal)
        newVal == "/home" ? (this.flag = false) : (this.flag = true)
      }
    }
  },
  computed: {
    // 三、通过计算属性获取购物车总数量
    // 3.1 按需导入getter,并使用
    ...mapGetters(["totalCount"])
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding-top: 40px;
  padding-bottom: 50px;
  // 组件切换瞬间,页面宽度会变成正常的两倍,此时需要隐藏超出区域
  overflow: hidden;
}
// 一、定义动画
.v-enter {
  opacity: 0;
  transform: translateX(100%);
}
.v-leave-to {
  opacity: 0;
  transform: translateX(-100%);
  position: absolute;
}
.v-enter-active,
.v-leave-active {
  transition: all 0.4s ease;
}

// 二、将 mui-tab-item 类名修改为 my-tab-item 解决js行为冲突,同时保持原来的样式,
// 在浏览器中检查样式,将有 mui-tab-item 类名的样式复制下来
.mui-bar-tab .my-tab-item.mui-active {
  color: #007aff;
}
.mui-bar-tab .my-tab-item {
  display: table-cell;
  overflow: hidden;
  width: 1%;
  height: 50px;
  text-align: center;
  vertical-align: middle;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #929292;
}
.mui-bar-tab .my-tab-item .mui-icon {
  top: 3px;
  width: 24px;
  height: 24px;
  padding-top: 0;
  padding-bottom: 0;
}
.mui-bar-tab .my-tab-item .mui-icon ~ .mui-tab-label {
  font-size: 11px;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
